{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\" seamless=\"seamless\" src=\"https://plot.ly/~mpyrcz/30.embed\" height=\"525px\" width=\"100%\"></iframe>"
      ],
      "text/plain": [
       "<plotly.tools.PlotlyDisplay object>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Example of an Python, Plotly Interactive Plot for Gaussian Transform of a Dataset\n",
    "# Michael Pyrcz, the University of Texas at Austin, Twitter @ GeostatsGuy\n",
    "#\n",
    "# To be included as a interactive visualization for a section on Gaussian Transforms for the www.GeostatisticalLessons.com\n",
    "# Coauthor on the section is Prof. Clayton Deustsch (the University of Alberta). Appreciation to Dr. Ryan Barnett for \n",
    "# excellent assistance providing example code and debugging an issue.\n",
    "# Editor for GeostatisticalLessons is Dr. Jared Deutsch (http://jareddeutsch.com/about.html).\n",
    "\n",
    "# Imports\n",
    "import copy\n",
    "import plotly.plotly as py\n",
    "import plotly.figure_factory as ff\n",
    "import plotly\n",
    "import pandas as pd\n",
    "import numpy\n",
    "import os\n",
    "import sys\n",
    "import plotly.graph_objs as go\n",
    "from plotly import tools\n",
    "from plotly import __version__\n",
    "from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot\n",
    "\n",
    "# Load Data - available in the GeostatsGuy/GeoDataSets repository of GitHub.\n",
    "os.chdir(\"C:/PGE337/\")\n",
    "data = pd.read_csv(\"NSCORE_Data.csv\")\n",
    "\n",
    "\n",
    "# Create the dynamic and static part of the plot\n",
    "\n",
    "line1x = numpy.zeros(7)\n",
    "line1y = numpy.zeros(7)\n",
    "dyn_data = []\n",
    "vis = True\n",
    "\n",
    "# Make trace 1 - dynamic transform lines, trace 2 - static original CDF, and trace 3 - static N[0,1] CDF\n",
    "ndata = 104\n",
    "for i in range(ndata):\n",
    "    if i > 0:\n",
    "        vis = False\n",
    "    if i > 0 and i < ndata:\n",
    "        pos = 'top'\n",
    "    elif i == ndata-1:\n",
    "        pos = 'top left'\n",
    "        \n",
    "    line1x[0] = data.iloc[i]['Porosity']\n",
    "    line1x[1] = data.iloc[i]['Porosity']\n",
    "    line1x[2] = data.iloc[i]['Nporosity']\n",
    "    line1x[3] = data.iloc[i]['Nporosity']\n",
    "    line1x[4] = data.iloc[i]['Nporosity'] + 0.09\n",
    "    line1x[5] = data.iloc[i]['Nporosity'] - 0.09\n",
    "    line1x[6] = data.iloc[i]['Nporosity']    \n",
    "    line1y[0] = 0.0\n",
    "    line1y[1] = data.iloc[i]['Prob']\n",
    "    line1y[2] = data.iloc[i]['Prob']\n",
    "    line1y[3] = 0.0\n",
    "    line1y[4] = 0.04 \n",
    "    line1y[5] = 0.04\n",
    "    line1y[6] = 0.0    \n",
    "    \n",
    "    trace1 = go.Scatter(\n",
    "            mode = 'lines',\n",
    "            visible = vis,\n",
    "            line=dict(color='black', width=3),\n",
    "            name = 'NSCORE Transform',\n",
    "            x = copy.deepcopy(line1x),\n",
    "            y = copy.deepcopy(line1y))\n",
    "    dyn_data.append(trace1)\n",
    "    trace2 = go.Scatter(\n",
    "            mode = 'lines+markers',\n",
    "            visible = vis,\n",
    "            line=dict(color=\"Red\", width=3),\n",
    "            name = 'Porosity CDF',\n",
    "            x = data['Porosity'],\n",
    "            y = data['Prob'])\n",
    "    dyn_data.append(trace2)\n",
    "    trace3 = go.Scatter(\n",
    "            mode = 'lines+markers',\n",
    "            visible = vis,\n",
    "            line=dict(color=\"Blue\", width=3),\n",
    "            name = 'N[Porosity] CDF',\n",
    "            x = data['Nporosity'],\n",
    "            y = data['Prob'])\n",
    "    dyn_data.append(trace3)    \n",
    "    \n",
    "# Define steps to link to slider\n",
    "steps = []\n",
    "j = 0\n",
    "for i in range(ndata):\n",
    "    step = dict(\n",
    "        method = 'restyle',\n",
    "        args = ['visible', [False] * len(dyn_data)],\n",
    "        label = str(data.iloc[i]['Porosity']),\n",
    "    )\n",
    "    step['args'][1][j] = True # Trace 1\n",
    "    j += 1\n",
    "    step['args'][1][j] = True # Trace 2\n",
    "    j += 1\n",
    "    step['args'][1][j] = True # Trace 3\n",
    "    j += 1    \n",
    "    steps.append(step)\n",
    "\n",
    "sliders = [dict(\n",
    "    active = 0,\n",
    "    currentvalue = {\"prefix\": \"Porosity Value: \"},\n",
    "    pad = {\"t\": ndata},\n",
    "    steps = steps\n",
    ")]   \n",
    "    \n",
    "# Edit the plot layout\n",
    "layout = dict(title = 'Demonstration of Transforming Data to Gaussian Distribution', \n",
    "              xaxis = dict(title = 'N[Porosity] (blue) and Porosity (red)',\n",
    "                          range = [-3, 21]),\n",
    "              yaxis = dict(title = 'Cumulative Probability',\n",
    "                          range = [0, 1.0]),\n",
    "              sliders = sliders\n",
    "              )\n",
    "\n",
    "fig = go.Figure(data=dyn_data, layout=layout)\n",
    "\n",
    "py.iplot(fig, filename='NSCORE_test')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
